<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <div id="sse">
    <a href="javascript:WebSocketTest()">运行 WebSocket</a>
  </div>
</body>
<script>
  function WebSocketTest() {
    if ("WebSocket" in window) {
      alert("您的浏览器支持WebSocket!");

      //打开一个web web_socket
      var ws = new WebSocket("ws://localhost:8080/websocket");

      ws.onopen = function () {
        //Web Socket已连接上，使用send()方法发送数据
        ws.send("发送数据test");
        alert("数据发送中...");
      };

      ws.onerror = function (ev) {
        alert("连接异常");
        console.log(ev);
      }

      ws.onmessage = function (evt) {
        var received_msg = evt.data;
        console.log(received_msg);
        alert("数据已接收...");
      };
      ws.onclose = function () {
        //关闭websocket
        alert("连接已关闭...");
      }
    } else {
      //浏览器不支持WebSocket
      alert("您的浏览器不支持 WebSocket!");
    }
  }
</script>
</html>